package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;
import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.zhy.autolayout.AutoRelativeLayout;

import org.w3c.dom.Text;

import java.net.HttpURLConnection;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.QrScanRes;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseActivity;

/**
 * Created by XPS on 2017/6/8.
 */

public class JoinCenterActivity extends BaseActivity {
    AutoRelativeLayout ar_search, ar_scan_qrcode, ar_share_qrcode;

    @Override
    protected int getLayout() {
        return R.layout.activity_joincenter;
    }

    @Override
    protected void setData() {
        ((TextView) r2v(R.id.actionbar_tv_title)).setText("添加");
        r2v(R.id.actionbar_iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void init() {
        ar_search = r2v(R.id.ar_search);
        ar_scan_qrcode = r2v(R.id.ar_scan_qrcode);
        ar_share_qrcode = r2v(R.id.ar_share_qrcode);
        ar_search.setOnClickListener(this);
        ar_scan_qrcode.setOnClickListener(this);
        ar_share_qrcode.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            if(!TeamHelper.getIns().hasTeam())return;
            getTeaminfowithTeamId(scanResult);
        }
    }

    void getTeaminfowithTeamId(String id){
        String data = URLConstants.TEAM.GETTEAMBY_QR_CODE+"?teamid="+id;
        OkHttpClientUtils.getDataAsync(this, data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
                JUtils.Toast("扫描失败");
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if(HttpResponsHelper.SuccessResponce(response)){
                    Logger.d(response.getBody());
                    QrScanRes qr = new Gson().fromJson(response.getBody(),QrScanRes.class);
                    Intent i = new Intent(JoinCenterActivity.this,JoinTeamActivity.class);
                    i.putExtra("Search.ListBean",qr.getObj());
                    startActivity(i);
                }else {
                    JUtils.Toast("扫描失败");
                }
            }

            @Override
            public void onStart() {

            }
        },"");
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ar_search:
                startActivity(new Intent(JoinCenterActivity.this, SearchActivity.class));
                break;
            case R.id.ar_scan_qrcode:
                Intent i = new Intent(this, CaptureActivity.class);
                startActivityForResult(i, 0);
                break;
            case R.id.ar_share_qrcode:
                startActivity(new Intent(JoinCenterActivity.this, InviteActivity.class));
                break;
        }
    }
}
